<?php

/**
 * Pes for PHP 5.3+
 *
 * Copyright (c) 2013 phpeskill.com (http://www.pescms.com)
 *
 * For the full copyright and license information, please view
 * the file LICENSE.md that was distributed with this source code.
 */

namespace App\Home;

use Model\Admin\FieldModel as FieldModel;
use Model\Admin\CategoryModel as CategoryModel;
use Core\Db\Db as Db;
use Expand\Home\Page as Page;
use Expand\Verify as Verify;

/**
 * Pes 内容调用模块
 * @author LazyPHP
 * @version 1.0
 */
class Content extends Common {

    /**
     * 构造函数
     */
    public function __construct() {
        parent::__construct();
    }

    /**
     * 方法跳转和执行list方法
     * @param type $name
     * @param type $arguments
     */
    public function __call($name, $arguments) {
        switch ($name) {
            case 'list':
                $this->_list();
                break;
            case 'sitemap':
                $this->display('sitemap');
                break;
            default :
                header('HTTP/1.1 404');
                $this->assign('title', 'Error 404(Not Found)!');
                $this->display('404');
        }
    }

    /**
     * 内容列表
     */
    public function _list() {
        $catid = $this->is_g('id', '请选择查看的分类');
        $data['catid'] = $this->Categorys[$catid]['child'];
        $db = $this->db(strtolower(MODULE));

        $page = new Page;
        $total = count($db->where("catid in ({$data['catid']}) and status = 1")->order('listsort asc, id desc')->select($data));
        $count = $page->total($total);
        $page->handle();
        $list = $db->where("catid in ({$data['catid']}) and status = 1")->order('listsort asc, id desc')->limit("{$page->firstRow}, {$page->listRows}")->select($data);

        $show = $page->show();
        $this->assign('keyword', $this->Categorys[$catid]['keyword']);
        $this->assign('description', $this->Categorys[$catid]['description']);
        $this->assign('page', $show);
        $this->assign('list', $list);
        $this->display(MODULE . "_list");
    }

    /**
     * 内容详细
     */
    public function view() {
        $id = $this->is_g('id', '请选择您要查看的内容');
        $db = $this->db(strtolower(MODULE));
        $list = $db->where('id = :id and status = 1')->find(array('id' => $id));
        $this->determineSqlExecResult($list, '您所查看的内容不存在或者被屏蔽');
        foreach ($list as $key => $value) {
            $this->assign($key, $value);
        }
        $this->display(MODULE . "_view");
    }

    public function verify() {
        $verify = new Verify;
        $verify->createVerify();
    }

}